const app = getApp(), fetchApi = require("../../utils/httpPost.js"), ad = require("../../template/ad/adTemplate.js"),
commonJs = require("../../component/common.js");
import Toast from '../../component/vant/toast/toast';
const CryptoJS = require("../../utils/aesUtil.js")
Page({
  data: {
    imgurl: app.getImgUrl(),
    userInfo: {},
    hasUserInfo: false,
    is_mer: 0,
    is_admin: 0,
    orderItemArr: [],
    adBanner:[],
    hasBanner:false,
    yuedou:0,
    shop_type: 0,
    isLogin:0,
    phone_num:'',
    isBandPhone:0,
    property_no_pay:0,
    egg_count:0,
    is_open_sign:0,
    is_show_mer:0,
    is_sign:0,
    device_count:0,
    is_show_repair:0,
    isiphonex: app.globalData.isiphonex,
    isxiaomi: app.globalData.isxiaomi,
    voucherMoneyTotal:0,
    car_count:0,
    ts_count:0,
    tsby_count:0,
    repair_count:0,
    estate_user_count:0,
    is_open_cash:0,
    is_test_user:0,
    goldenMoney:0,
    vipAccountMoney:0,
    is_show_wx_ad:0,
    nick_name:'',
    face_url:'',
    user_no:'',
    order_coupon_ount:'',
    showQr:false,
    promotion_qr_code:'',
    is_bingge_promotion:0,
    bucketCount:0,
    deposit:0,
    is_bg_sale_user:0,
  },
  //事件处理函数
  bindViewTap: function() {
    var isLogin = app.getStorageSync("isLogin");
    if(isLogin){
      wx.navigateTo({ url: '/pages/account/index' })
    }else{
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }
  },
  onLoad: function () {
    // this.doLogin()
  },
  goLogin:function(){
    wx.navigateTo({
      url: '/pages/login/index',
    })
  },
  doLogin: function(){
    var that = this
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log("code", res.code)
        fetchApi.httpPost('/user/wxLogin', { code: res.code }, function (res) {
          console.log("登录返回数据", res)
          if(res.data.code == 0){
            // that.onHttpGetUserInfo()
          }
          that.setData({ isLoading: false, loading: true })
        }, function (res) { 
          that.setData({ isLoading: false, loading: true })
        })
      },fail(res){
        that.setData({ isLoading: false, loading: true })
      }
    })
  },
  goCopy(e){
    console.log('goCopy')
    var that = this;
    wx.setClipboardData({
      data: that.data.user_no,
      success: function(res) {
        console.log('res', res)
        wx.showToast({
          title: '复制成功',
          icon: 'success',
          duration: 2000
        });
      },fail(e){
        console.log('fail', e)
      }
    });
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/user/getUserInfoData', {
    }, function (res) {
      console.log("getUserInfoData 数据", res)
      if (res.data.code == 0) {
        console.log('device_count',res.data.data.device_count);
        that.setData({ 
          orderItemArr: res.data.data.orderItemArr,
          yuedou: res.data.data.yuedou != undefined ? res.data.data.yuedou:0,
          phone_num: res.data.data.phone_num!=null&&res.data.data.phone_num.length > 0 ? res.data.data.phone_num :'请绑定手机号',
          isBandPhone:res.data.data.phone_num!=null&&res.data.data.phone_num.length > 0?1:0,
          property_no_pay: res.data.data.property_no_pay,
          is_mer: res.data.data.is_mer,
          is_mer_egg: res.data.data.is_mer_egg,
          is_admin: res.data.data.is_admin,
          is_open_sign:res.data.data.is_open_sign,
          is_show_mer:res.data.data.is_show_mer,
          is_sign:res.data.data.is_sign,
          is_test_user: res.data.data.is_test_user,
          device_count:res.data.data.device_count,
          egg_count:res.data.data.egg_count,
          is_show_repair:res.data.data.is_show_repair,
          voucherMoneyTotal:res.data.data.voucherMoneyTotal,
          car_count:res.data.data.car_count,
          ts_count:res.data.data.ts_count,
          tsby_count:res.data.data.tsby_count,
          repair_count:res.data.data.repair_count,
          estate_user_count:res.data.data.estate_user_count,
          is_open_cash: res.data.data.is_open_cash,
          goldenMoney:res.data.data.goldenMoney, 
          vipAccountMoney:res.data.data.vipAccountMoney, 
          is_show_wx_ad:res.data.data.is_show_wx_ad, 
          nick_name:res.data.data.nick_name, 
          face_url :res.data.data.face_url,
          user_no :res.data.data.user_no,
          order_coupon_ount:res.data.data.order_coupon_ount,
          promotion_qr_code:res.data.data.promotion_qr_code,
          is_bingge_promotion:res.data.data.is_bingge_promotion,
          bucketCount:res.data.data.bucketCount,
          deposit:res.data.data.deposit,
          is_bg_sale_user:res.data.data.is_bg_sale_user,
        })
        if(res.data.data.phone_num!=null && res.data.data.phone_num.length > 0){
          app.setStorageSync("phone_num", res.data.data.phone_num);
        }
        if (!that.data.hasBanner) {
          that.setData({
            adBanner: res.data.data.banner,
            hasBanner: res.data.data.hasBanner
          })
        }
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    },function(){})
  },
  onShow: function () {
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    console.log('isLogin', isLogin)
    var is_mer = app.getStorageSync("is_mer");
    var is_admin = app.getStorageSync("is_admin");
    that.setData({
      isLogin: isLogin,
      is_mer: is_mer,
      is_admin: is_admin
    })
    app.globalData.userInfo = app.getStorageSync("userInfo");
    console.log('app.globalData.userInfo', app.globalData.userInfo)
    if (isLogin) {
      that.setData({
        userInfo: app.globalData.userInfo
      })
      that.onHttpInit()
    }
    app.getCurrentTabbar(2,this);
  },
  shop_order:function(e){
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  pushToAd: function (e) {
    console.log('pushToAd e', e)
    ad.pushToAd(e, 'personal')
  },
  goFarmer(){
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else{
      wx.switchTab({
        url: '/pages/farmer/info/index',
      })
    }
  },
  goBuyBucket(){
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else{
      wx.navigateTo({
        url: '/pages/bucket/buy/index',
      })
    }
  },
  goGolden(){
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else{
      wx.navigateTo({
        url: '/pagesFarm/pages/goldenManage/index',
      })
    }
  },
  goIntegralRecord:function(){
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else{
      wx.navigateTo({
        url: '/pages/yd/account/index?is_open_cash=' +that.data.is_open_cash,
      })
    }
  },
  goVoucher(){
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else{
      wx.navigateTo({
        url: '/pages/coupon/voucher/index',
      })
    }
  },
  getQrcode:function(e){
    console.log('pushToQrCode e', e)
    wx.navigateTo({
      url: '/pages/coupon/previewQrcode/index?qrcode_type=personal'
    })
  },
  goSign:function(){
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else{
      wx.navigateTo({
        url: '/pagesFarm/pages/sign/index'
      })
    }
  },
  goOrderCoupon(){
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else{
      wx.navigateTo({
        url: '/pagesMer/pages/orderCoupon/index'
      })
    }
  },
  goVip(){
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else{
      // wx.navigateTo({
      //   url: '/pagesMer/pages/vip/index'
      // })
      wx.navigateTo({
        url: '/pagesMer/pages/selfVip/list/index'
      })
      
    }
  },
  showQrCode(){
    this.setData({
      showQr : !this.data.showQr,
    })
  },
  callPlatform(e){
    wx.makePhoneCall({
      phoneNumber: '4006860633',
    })
  },
  goScanRecharge(res){
    console.log('scanCode res',res)
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    that.data.from_page = 'scancode'
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else {
      wx.scanCode({
        success: (res) => {
          console.log("扫码结果", res);
          var qrcontent = res.result;
          console.log('扫码结果',qrcontent)
          var q = "?" + qrcontent.split("?")[1];

          // var json_param = commonJs.getQueryVariable(q);
          // var linkParam = "qrtype=device&id=" + json_param.id;
          // console.log('linkParam',linkParam)
          if(qrcontent.indexOf('https://') >= 0){
            var q = "?" + qrcontent.split("?")[1];
            var json_param = commonJs.getQueryVariable(q);
            if(qrcontent.indexOf('water/card/n') >= 0){//扫描水卡绑定二维码
              wx.navigateTo({
                url: '/pages/water/recharge/sale/index?card_num=' + json_param.n,
              })
            }  else {
              Toast.fail("当前二维码无效");
            }
          }
        },
        fail: (res) => { console.log(res); }
      })
    }
  },
  /**
   * 管理员设备激活
   * @param {} res 
   */
  scanCodeDevice: function (res) {
    console.log('scanCode res',res)
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    that.data.from_page = 'scancode'
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else {
      wx.scanCode({
        success: (res) => {
          console.log("扫码结果", res);
          var qrcontent = res.result;
          console.log('扫码结果',qrcontent)
          var q = "?" + qrcontent.split("?")[1];
          var json_param = commonJs.getQueryVariable(q);
          var linkParam = "qrtype=device&id=" + json_param.id;
          console.log('linkParam',linkParam)
          var device_id = json_param.id
          if(json_param.isEgg){
            linkParam += "&isEgg="+json_param.isEgg;
          }
          that.deviceOp(linkParam,device_id)
        },
        fail: (res) => { console.log(res); }
      })
    }
  },
  scanCode: function (res) {
    console.log('scanCode res',res)
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    that.data.from_page = 'scancode'
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    } else {
      wx.scanCode({
        success: (res) => {
          console.log("扫码结果", res);
          var qrcontent = res.result;
          console.log('扫码结果',qrcontent)
          var json_param = {};
          if(qrcontent.indexOf('https://') >= 0){
            var q = "?" + qrcontent.split("?")[1];
            var json_param = commonJs.getQueryVariable(q);
            if(qrcontent.indexOf('shop/getEgg') >= 0){
              wx.navigateTo({ url: '/pagesMer/pages/payEgg/index?no=' + json_param.no})
            } else{
              Toast.fail("当前二维码无效");
            }
          }
        },
        fail: (res) => { console.log(res); }
      })
    }
  },
})
